package com.ruantu.clothing.service.impl;

import com.ruantu.clothing.dao.JgMapper;
import com.ruantu.clothing.dao.ZhekoucelueMapper;
import com.ruantu.clothing.entity.Jg;
import com.ruantu.clothing.entity.Pifashang;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.Zhekoucelue;
import com.ruantu.clothing.service.JgService;
import com.ruantu.clothing.util.MyException;
import com.ruantu.clothing.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/6/14 0014.
 */
@Service
public class JgServiceImpl implements JgService {
    @Autowired
    private JgMapper jgMapper;
    @Autowired
    private ZhekoucelueMapper zhekoucelueMapper;

    @Override
    public List<Jg> findTree(Jg jg) {

        return jgMapper.getAllJD(jg);
    }

    @Override
    public List<Jg> findList(Jg jg) {
        return jgMapper.findList(jg);
    }

    @Override
    public int findJgname(Jg jg) {
        int count = jgMapper.findCountByName(jg);
        if (count > 0) {
            throw new MyException("已存在该机构，请检查重试");
        }
        return count;
    }

    @Override
    public int saveJg(Jg jg) {
        Zhekoucelue zhekoucelue = new Zhekoucelue();
        zhekoucelue.setId(UUIDUtil.getUUID());
        zhekoucelue.setJgId(jg.getJgId());
        zhekoucelue.setFenxiaoZhekou(jg.getZhekou());
        //修改
        if (jg.getId() != null && !"".equals(jg.getId())) {
            int code = jgMapper.findCountByName(jg);
            if (code > 0) {
                throw new MyException("修改失败，已存在相同的机构");
            }
            int codes = jgMapper.updateByPrimaryKeySelective(jg);
            if (codes <= 0) {
                throw new MyException("修改失败，请确认信息重新尝试");
            }
            //查询是否存在该折扣  没有增加  有更新
            zhekoucelue.setKehuid(jg.getId());
            int count = zhekoucelueMapper.findCountByJgidAndKehuId(zhekoucelue);
            //存在更新
            if (count > 0) {
                zhekoucelueMapper.updateZhekou(zhekoucelue);
            } else {
                zhekoucelueMapper.insertSelective(zhekoucelue);
            }
            return codes;
        } else {
            //新增
            //查询是否存在该供应商
            int count = jgMapper.findCountByName(jg);
            if (count > 0) {
                throw new MyException("新增失败，已存在相同的机构");
            }
            jg.setId(UUIDUtil.getUUID());
            int code = jgMapper.insertSelective(jg);
            if (code <= 0) {
                throw new MyException("新增失败，请检查数据重复");
            }
            zhekoucelue.setKehuid(jg.getId());
            //增加折扣策略表
            code = zhekoucelueMapper.insertSelective(zhekoucelue);
            if (code <= 0) {
                throw new MyException("保存失败，请检查数据重新尝试");
            }
            return code;
        }
    }

    @Override
    public Jg findJgById(Jg jg) {
        Jg jg1 = jgMapper.findJgById(jg);
        if (jg1 == null) {
            throw new MyException("修改数据不存在，请确认信息重试");
        }
        return jg1;
    }

    @Override
    public int delJg(Jg jg) {
        int code = jgMapper.delJg(jg);
        if (code <= 0) {
            throw new MyException("删除失败，请确认重新尝试");
        }
        return code;
    }


    /***
     * 根据类型获得分销商客户
     * @param u
     * @return
     */
    @Override
    public List<Pifashang> selectJGforLeiXingService(User u) {

        Map<String, Object> mapCan = new HashMap<>();
        mapCan.put("jgId", u.getJgid());
        mapCan.put("jglx", u.getJgleixing());
        mapCan.put("parenId", u.getParentId());

        return jgMapper.selectJGforLeiXing(mapCan);
    }
}